package com.dudu.utils;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class PhoneUtils {

    private static String[] fbsArr = { "\\", "$", "(", ")", "*", "+", ".", "[", "]", "?", "^", "{", "}", "|", "%", "&", " " };
    // 禁用的号码开头
    private static String[] issueNum = { " 00", " 860", " 8617909", "+00", "+860", "+8617909", "00", "860", "861790900" };

    public static void main(String[] args) {

	System.out.println(PhoneUtils.isMobileOrPhone("12345"));
    }

    /**
     * 验证是不是电话号码
     * 
     * @return
     */
    public static boolean isMobileOrPhone(String phone) {
	if (isMobilePhone(phone) || isPhone(phone)) {
	    return true;
	}
	return false;
    }

    /**
     * 验证是否是手机号
     * 
     * @param phone
     * @return
     */
    public static boolean isMobilePhone(String phone) {
	if (isNull(phone)) {
	    return false;
	}
	Pattern p = Pattern.compile("^[1][3,4,5,7,8,9][0-9]{9}$");
	Matcher m = p.matcher(phone);
	return m.matches();
    }

    /**
     * 正则验证固定电话
     * 
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
	if (StringUtils.isBlank(phone)) {
	    return false;
	}
	Pattern p = Pattern.compile("^((0\\d{2,3}))?\\d{7,8}(a\\d{3,4})?$");
	Matcher m = p.matcher(phone);
	return m.matches();
    }

    /**
     * 判断手机号码是否为禁用的号码开头
     * 
     * @param phoneNums
     * @return
     */
    public static boolean filterIssueNumber(String phoneNums) {
	// 用于最后返回值
	boolean resultIssue = true;
	// 判断传入的值是否为空
	if (StringUtils.isNotBlank(phoneNums)) {
	    // 拆分号码
	    String[] phoneNumber = phoneNums.split(",");
	    // 遍历手机号码是否为
	    for (String number : phoneNumber) {
		// 循环遍历处理特殊字符
		for (String key : issueNum) {
		    // 判断手机号码是否为key开头
		    if (number.startsWith(key)) {
			resultIssue = false;
			return resultIssue;
		    }
		}
	    }
	} else {
	    resultIssue = false;
	}

	return resultIssue;
    }

    /**
     * 本工具类是用于处理发起通话中的手机号码的特殊符号，和相应需要过滤的号码
     * 
     * @param phoneNums
     *            号码字符串，多个号码间使用‘,’分割
     * @return
     */
    public static String filterMobileNumber(String phoneNums) {
	String phoneNumsResult = "";
	// 循环遍历处理特殊字符
	for (String key : fbsArr) {
	    if (phoneNums.contains(key)) {
		phoneNums = phoneNums.replace(key, "");
	    }
	}
	if (StringUtils.isNotBlank(phoneNums)) {
	    // 拆分号码
	    String[] phoneNumber = phoneNums.split(",");
	    for (int i = 0; i < phoneNumber.length; i++) {
		// 获取号码
		String tmpNumber = phoneNumber[i];
		if (i == 0) {
		    if (tmpNumber.startsWith("+86")) {
			phoneNumsResult = tmpNumber.substring(3, tmpNumber.length());
		    } else {
			if (tmpNumber.startsWith("86")) {
			    phoneNumsResult = tmpNumber.substring(2, tmpNumber.length());
			} else {
			    phoneNumsResult = tmpNumber;
			}
		    }
		} else {
		    if (tmpNumber.startsWith("+86")) {
			phoneNumsResult = phoneNumsResult + "," + tmpNumber.substring(3, tmpNumber.length());
		    } else {
			if (tmpNumber.startsWith("86")) {
			    phoneNumsResult = phoneNumsResult + "," + tmpNumber.substring(2, tmpNumber.length());
			} else {
			    phoneNumsResult = phoneNumsResult + "," + tmpNumber;
			}
		    }
		}

	    }
	} else {
	    phoneNumsResult = "";
	}
	return phoneNumsResult;
    }

    /**
     * 判断手机号是否为空
     * 
     * @param phone
     * @return
     */
    public static boolean isNull(String phone) {

	if (phone.equals("") || phone == null) {
	    return true;
	}
	return false;

    }

    /**
     * 手机号去重复
     * 
     * @param phones
     *            手机号码，多个号码之间用‘,’分割
     * @return
     */
    public static String phoneNumDereplication(String phones) {
	String phoneNumsResult = "";
	if (StringUtils.isNotBlank(phones)) {
	    // 拆分号码
	    String[] phoneNumber = phones.split(",");

	    List<String> list = new LinkedList<String>();
	    // 开始去重
	    for (int i = 0; i < phoneNumber.length; i++) {
		if (!list.contains(phoneNumber[i])) {
		    list.add(phoneNumber[i]);
		    if (i == 0) {
			phoneNumsResult = phoneNumber[i];
		    } else {
			phoneNumsResult = phoneNumsResult + "," + phoneNumber[i];
		    }
		}
	    }
	}
	return phoneNumsResult;
    }
}
